home *** CD-ROM | disk | FTP | other *** search
/ Amiga Format CD 31 / Amiga Format CD31 (1998-09-02)(Future Publishing)(GB)(Track 1 of 2)[!][issue 1998-10].iso / -seriously_amiga- / hardware / transadf / source / main.c < prev    next >
C/C++ Source or Header  |  1998-07-20  |  9KB  |  350 lines

  1. /* main.c - Entry point plus startup and shutdown routines
  2. ** Copyright (C) 1997,1998 Karl J. Ots
  3. ** 
  4. ** This program is free software; you can redistribute it and/or modify
  5. ** it under the terms of the GNU General Public License as published by
  6. ** the Free Software Foundation; either version 2 of the License, or
  7. ** (at your option) any later version.
  8. ** 
  9. ** This program is distributed in the hope that it will be useful,
  10. ** but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12. ** GNU General Public License for more details.
  13. ** 
  14. ** You should have received a copy of the GNU General Public License
  15. ** along with this program; if not, write to the Free Software
  16. ** Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  17. */
  18.  
  19. /*----------------*/
  20. /* Main procedure */
  21. /*----------------*/
  22.  
  23. /* COMPILE_LITE takes precedence over COMPILE_RT */
  24. #ifdef COMPILE_LITE
  25. #  ifdef COMPILE_RT
  26. #    undef COMPILE_RT
  27. #  endif /* COMPILE_RT */
  28. #endif /* COMPILE_LITE */
  29.  
  30. #include <exec/types.h>
  31. #include <exec/ports.h>
  32. #include <exec/io.h>
  33. #ifdef COMPILE_RT
  34. #  include <exec/libraries.h>
  35. #endif /* COMPILE_RT */
  36. #include <devices/trackdisk.h>
  37. #include <dos/dos.h>
  38. #include <dos/dosextens.h>
  39. #include <clib/exec_protos.h>
  40. #include <clib/dos_protos.h>
  41.  
  42. #include <fcntl.h>
  43. #include <stdio.h>
  44. #include <stdlib.h>
  45.  
  46. #include "main.h"
  47. #include "args.h"
  48. #include "mem_chunks.h"
  49. #include "td.h"
  50. #include "read_disk.h"
  51. #include "write_disk.h"
  52. #ifndef COMPILE_LITE
  53. #  include "defl_disk.h"
  54. #  include "infl_disk.h"
  55. #endif /* COMPILE_LITE */
  56. #include "util.h"
  57. #include "errors.h"
  58. #include "version.h"
  59.  
  60.  
  61. /*---------------------------------*/
  62. /* Global constants and structures */
  63. /*---------------------------------*/
  64.  
  65. const char breakText[]   = "***Break";
  66.  
  67. /*----------------------------*/
  68. /* Standatd Input/Ouput/Error */
  69. /*----------------------------*/
  70.  
  71. BPTR StdIn, StdOut, StdErr;
  72. STRPTR ProgName;
  73.  
  74. /*
  75. ** Set handles for DOS Library StdIO functions, as well as
  76. ** find our command name.
  77. */
  78. void setStdIO (void)
  79. {
  80.   struct Process *Proc = (struct Process *)FindTask (NULL);
  81.   
  82.   StdIn  = Proc->pr_CIS;  /* Quicker than Input() or Output() */
  83.   StdOut = Proc->pr_COS;
  84.   StdErr = Proc->pr_CES;
  85.   if (!StdErr)
  86.   {
  87.     /* Use the file handle from the file number of stdio FILE stderr */
  88.     StdErr = (BPTR) fdtofh (fileno (stderr));
  89.     if (!StdErr) StdErr = StdOut;
  90.   }
  91. }
  92.  
  93.  
  94. /*-------------------------------------------*/
  95. /* Global variables and associated functions */
  96. /*-------------------------------------------*/
  97.  
  98. #ifdef COMPILE_RT
  99. /* z.library base pointer */
  100. struct Library *ZBase;
  101. #endif /* COMPILE_RT */
  102.  
  103. /* files */
  104. BPTR   ADFile;
  105. STRPTR DOSDev;
  106.  
  107. /* io */
  108. struct MsgPort *diskPort;
  109. struct IOExtTD *diskReq;
  110. BOOL   TDOpen;
  111.  
  112.  
  113. /*
  114. ** Initialise all globals to their default values.
  115. */
  116. void initGlobals (void)
  117. {
  118. #ifdef COMPILE_RT
  119.   ZBase = NULL;
  120. #endif /* COMPILE_RT */
  121.   
  122.   setStdIO();
  123.   
  124.   /* Iniitalise our memory list */
  125.   initMemChunkList();
  126.   
  127.   ADFile = NULL;
  128.   DOSDev = NULL;
  129.   
  130.   diskPort = NULL;
  131.   diskReq = NULL;
  132.   TDOpen = FALSE;
  133. }
  134.  
  135.  
  136. /*
  137. ** Exit cleanly.
  138. */
  139. void cleanExit (ULONG rc, LONG rc2)
  140. {
  141.   /* Free all allocated memory */
  142.   deleteMemChunkList();
  143.   
  144.   if (TDOpen)
  145.   {
  146.     /* Flush any buffers that may be unwritten */
  147.     flushTrack (diskReq);
  148.     
  149.     /* Turn off the motor */
  150.     diskReq->iotd_Req.io_Command = TD_MOTOR;
  151.     diskReq->iotd_Req.io_Flags = 0;
  152.     diskReq->iotd_Req.io_Length = 0;
  153.     DoIO ((struct IORequest *)diskReq);
  154.     
  155.     CloseDevice ((struct IORequest *)diskReq);
  156.   }
  157.   
  158.   if (diskReq) DeleteIORequest ((APTR)diskReq);
  159.   if (diskPort) DeleteMsgPort (diskPort);
  160.   
  161.   if (DOSDev) Inhibit (DOSDev,FALSE);
  162.   if (ADFile) Close (ADFile);
  163.   
  164.   /* Turn the cursor on */
  165.   FPuts (StdOut, "\033[1 p"); 
  166.   Flush (StdOut);
  167.   
  168. #ifdef COMPILE_RT
  169.   if (ZBase) CloseLibrary (ZBase);
  170. #endif /* COMPILE_RT */
  171.   
  172.   SetIoErr (rc2);
  173.   exit (rc);
  174. }
  175.  
  176.  
  177. /*---------------*/
  178. /* Main function */
  179. /*---------------*/
  180. int main (int argc, char *argv[])
  181. {
  182.   struct ADF_Packet adf_packet = {NULL, NULL, NULL, NULL, NULL, NULL, FALSE};
  183.   struct TA_Args *taArgs;
  184.   
  185.   LONG   diskUnit;
  186.   STRPTR ADFileName, origName;
  187.   ULONG  startTrack, endTrack, cLevel;
  188.   ULONG  fileType = 0;
  189.   
  190.   BOOL   WriteDisk, Verify;
  191.   LONG   ADFOpenMode = MODE_NEWFILE;  /* Default is to write to file */
  192.   BYTE   TDError;
  193.   LONG   DOSError;
  194.   
  195.   
  196.   /* Initialise any global data */
  197.   initGlobals();
  198.   ProgName = FilePart (argv[0]);
  199.  
  200.   /* Collect arguments */
  201.   taArgs = getArgs ();
  202.   if (!taArgs) outputUsage ();
  203.  
  204. #ifdef COMPILE_RT
  205.   /* Open z.library */
  206.   ZBase = OpenLibrary ("z.library",0);
  207.   if (!ZBase)
  208.   {
  209.     FPrintf (StdErr, "%s: Couldn't open z.library.\n", ProgName);
  210.     cleanExit (RETURN_FAIL, NULL);
  211.   }
  212. #endif /* COMPILE_RT */
  213.  
  214.   diskUnit   = Name2Unit (taArgs->Drive);
  215.   ADFileName = taArgs->File;
  216.   startTrack = taArgs->Start;
  217.   endTrack   = taArgs->End;
  218.   WriteDisk  = taArgs->WriteDisk;
  219.   Verify     = taArgs->Verify;
  220.   
  221. #ifndef COMPILE_LITE
  222.   if      (taArgs->ZLib)  fileType = FT_ZLIB;  
  223.   else if (taArgs->GZip)  fileType = FT_GZIP;  
  224.   else if (taArgs->PKZip) 
  225.   {
  226.     if (taArgs->PKZAdd)   fileType = FT_PKZIP_ADD;
  227.     else                  fileType = FT_PKZIP;
  228.   }
  229.   origName = taArgs->OrigName;  
  230.   cLevel = taArgs->Level;
  231. #endif /* COMPILE_LITE */
  232.   
  233.   if (diskUnit < 0)
  234.   {
  235.     FPrintf (StdErr, "%s: Error - Invalid drive.\n",ProgName);
  236.     cleanExit (RETURN_FAIL, NULL);
  237.   }
  238.   if ((startTrack < 0) || (startTrack > endTrack))
  239.   {
  240.     FPrintf (StdErr, "%s: Invalid Start/End track.\n", ProgName);
  241.     cleanExit (RETURN_FAIL, NULL);
  242.   }
  243.   if (endTrack > 159)
  244.     FPrintf (StdErr, "%s: Warning - Extended tracks specified, transfer may fail.\n",ProgName);
  245.   
  246.   /* Inhibit DOS access to the drive we'll be operating on */  
  247.   DOSDev = taArgs->Drive;
  248.   if ( !Inhibit (DOSDev, DOSTRUE) )
  249.   {
  250.     DOSError = IoErr();
  251.     FPrintf (StdErr, "%s: Error - Couldn't inhibit DOS access to %s.\n",
  252.                      ProgName, DOSDev);
  253.     DOSDev = NULL;
  254.     cleanExit (RETURN_FAIL, DOSError);
  255.   }
  256.   
  257.   /* Open the Amiga Disk File */
  258.   if (WriteDisk) ADFOpenMode = MODE_OLDFILE;  /* Reading from file */
  259.   else if (fileType == FT_PKZIP_ADD) ADFOpenMode = MODE_READWRITE;
  260.   ADFile = Open (ADFileName, ADFOpenMode);
  261.   if (!ADFile)
  262.   {
  263.     DOSError = IoErr();
  264.     FPrintf (StdErr, "%s: Couldn't open %s for %s - ", ProgName, ADFileName,
  265.                      (WriteDisk ? "input" : "output"));
  266.     reportDOSError (DOSError);
  267.     cleanExit (RETURN_FAIL, DOSError);
  268.   }
  269.   
  270.   /* Attempt to create a MsgPort */
  271.   diskPort = CreateMsgPort();
  272.   if (!diskPort)
  273.   {
  274.     FPrintf (StdErr, 
  275.              "%s: Error - Couldn't create a Message Port for TrackDisk IO.\n",
  276.              ProgName);
  277.     cleanExit (RETURN_FAIL, NULL);
  278.   };
  279.   
  280.   /* Attempt to create an IORequest */
  281.   diskReq = (struct IOExtTD *)
  282.             CreateIORequest (diskPort, sizeof (struct IOExtTD));
  283.   if (!diskReq)
  284.   {
  285.     FPrintf (StdErr,
  286.              "%s: Error - Couldn't create a Request for TrackDisk IO.\n",
  287.              ProgName);
  288.     cleanExit (RETURN_FAIL, NULL);
  289.   }
  290.   
  291.   /* Attempt to open trackdisk.device */
  292.   TDError = OpenDevice (TD_NAME, diskUnit, (struct IORequest *)diskReq,NULL);
  293.   if (TDError)
  294.   {
  295.     FPrintf (StdErr, "%s: Couldn't open trackdisk.device - ", ProgName);
  296.     reportTDError (TDError);
  297.     cleanExit (RETURN_FAIL, NULL);
  298.   }
  299.   else
  300.     TDOpen = TRUE;
  301.   
  302.   /* The trackdisk.device in now open and ready for use */
  303.   
  304.   /* Turn off the cursor */
  305.   FPuts (StdOut, "\033[0 p"); Flush (StdOut);
  306.   
  307.   /* Load packet */
  308.   adf_packet.diskReq    = diskReq;
  309.   adf_packet.diskUnit   = diskUnit;
  310.   adf_packet.ADFile     = ADFile;
  311.   adf_packet.ADFileName = ADFileName;
  312.   adf_packet.startTrack = startTrack;
  313.   adf_packet.endTrack   = endTrack;
  314.   adf_packet.verify     = Verify;
  315.   
  316.   if (WriteDisk)
  317.   {
  318.     /* We're reading from the file and writing to the disk */
  319. #ifndef COMPILE_LITE
  320.     fileType = getFileType (ADFile);
  321.     
  322.     if ((fileType == FT_ZLIB) || 
  323.         (fileType == FT_GZIP) ||
  324.         (fileType == FT_PKZIP))
  325.       inflDisk (&adf_packet, origName, fileType);
  326.     else
  327. #endif /* COMPILE_LITE */
  328.       writeDisk (&adf_packet);
  329.   }
  330.   else
  331.   {
  332.     /* We're reading from the disk and writing to the file */
  333. #ifndef COMPILE_LITE
  334.     if (fileType)
  335.       deflDisk (&adf_packet, cLevel, origName, fileType);
  336.     else
  337. #endif /* COMPILE_LITE */
  338.       readDisk (&adf_packet);
  339.   }
  340.   
  341.   
  342.   FPuts (StdOut, "\nDone.\n");
  343.   
  344.   /* Let's get outa here! */
  345.   cleanExit (RETURN_OK, NULL);
  346.   
  347.   /* Keep the compiler happy */
  348.   return 0;
  349. }
  350.